//
//  XFVisitView.swift
//  SinaBlog
//
//  Created by macs on 15/11/8.
//  Copyright © 2015年 BlackTail. All rights reserved.
/* */

import UIKit
import SnapKit

@objc protocol VisitViewDelegate : NSObjectProtocol {
    
    optional func visitWillLogin()
    
    optional func visitWillRegist()
    
}

class XFVisitView: UIView {
    
    weak var visitViewDelegate : VisitViewDelegate?
    override init(frame: CGRect) {
        //
        super.init(frame: CGRectZero)
        //添加子控件
        self.backgroundColor = UIColor.whiteColor()
        setUpUI()
        
    }
    //该方法在重写init(frame: CGRect) 后,必须实现,表示如果使用story和xib就会报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func registBtnClick() {
        //这里的方法后面的?就是判断是否响应
        visitViewDelegate?.visitWillRegist?()
        
    }
    @objc private func loginBtnClick() {
        //这里的方法后面的?就是判断是否响应
        visitViewDelegate?.visitWillLogin?()
    }
    
    func setSubViewsProperty(imageName:String?,descri : String) {
        
        iconView.hidden = false
        descrLable.text = descri
        if imageName != nil {
            
            circleView.image = UIImage(named: imageName!)
            iconView.hidden = true
            bringSubviewToFront(circleView)
        } else {
            circleAnimate()
        }
    }
    
    private func circleAnimate() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 15
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setUpUI() {
        //添加子控件
        addSubview(circleView)
        addSubview(coverView)
        addSubview(iconView)
        addSubview(descrLable)
        addSubview(loginBtn)
        addSubview(registBtn)
        
        //布局子控件
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        //VFL
        //设置 circle
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        //设置 iconview
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.centerY.equalTo(circleView.snp_centerY)
        }
        
        // 设置描述信息
        
        descrLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom)
            make.height.equalTo(40)
            make.width.equalTo(224)
        }
        
        //设置登录按钮
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(descrLable.snp_left)
            make.top.equalTo(descrLable.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        //设置注册按钮
        registBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(descrLable.snp_right)
            make.top.equalTo(descrLable.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        //设置蒙版界面
        coverView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(loginBtn.snp_bottom)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top).offset(30)
        }
        
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        registBtn.addTarget(self, action: "registBtnClick", forControlEvents: .TouchUpInside)
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
    }
    
    //懒加载子控件
    lazy var circleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var descrLable : UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", size: 14.0, color: UIColor.lightGrayColor())
    
    lazy var loginBtn : UIButton = UIButton(title: "登录", color: UIColor.lightGrayColor(), icon: "common_button_white_disable")
    
    lazy var registBtn : UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), icon: "common_button_white_disable")
    
    lazy var coverView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
